package cn.zd.demo1;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

import cn.zd.demo1.widget.MyDialog;

public class DialogActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {

    private TextView textView;
    private TextView tv_dp;
    private TextView tv_tp;
    private DatePicker dp;
    private TimePicker tp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(this,this.getClass().getName(),Toast.LENGTH_LONG).show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        textView = findViewById(R.id.tv_default);
        tv_dp = findViewById(R.id.tv_dp);
        tv_tp = findViewById(R.id.tv_tp);
        dp = findViewById(R.id.dp_default);
        tp = findViewById(R.id.tp_default);
        findViewById(R.id.btn_d).setOnClickListener(this);
        findViewById(R.id.btn_d1).setOnClickListener(this);
        findViewById(R.id.btn_dp).setOnClickListener(this);
        findViewById(R.id.btn_dp_date).setOnClickListener(this);
        findViewById(R.id.btn_tp).setOnClickListener(this);
        findViewById(R.id.btn_tp_time).setOnClickListener(this);
        findViewById(R.id.btn_pg1).setOnClickListener(this);
        findViewById(R.id.btn_pg2).setOnClickListener(this);
        findViewById(R.id.btn_pg3).setOnClickListener(this);
        findViewById(R.id.btn_pg4).setOnClickListener(this);
        ProgressBar pb2 = findViewById(R.id.pb2);
        final int[] a = {0};
        new Thread(()->{
            while (a[0] <= 100) {
                pb2.setProgress(a[0]);
                a[0] +=10;
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    Log.e("sleep","sleeperror",e);
                }
            }
        }).start();

    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_d || v.getId() == R.id.btn_d1) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setIcon(R.drawable.android1);
            builder.setTitle("我是标题");
            builder.setMessage("我是内容");
            builder.setPositiveButton("确认Btn", ((dialog, which) -> {
                textView.setText(String.format("对话框选择了：%s", "确认Btn"));
            }));
            builder.setNegativeButton("取消Btn", ((dialog, which) -> {
                textView.setText(String.format("对话框选择了：%s", "取消Btn"));
            }));
            if (v.getId() == R.id.btn_d1) {
                builder.setNeutralButton("中性Btn", ((dialog, which) -> {
                    textView.setText(String.format("对话框选择了：%s", "中性Btn"));
                }));
            }
            builder.create().show();
        } else  if(v.getId() == R.id.btn_dp) {
            tv_dp.setText(String.format("DatePicker选择的日期是：%s年%s月%s日",dp.getYear(),dp.getMonth() +1 ,dp.getDayOfMonth()));
        } else  if(v.getId() == R.id.btn_tp) {
            tv_tp.setText(String.format("TimePicker选择的时间是：%s时%s分",tp.getHour(),tp.getMinute()));
        }else  if(v.getId() == R.id.btn_dp_date) {
            Calendar calendar = Calendar.getInstance();
            DatePickerDialog dialog = new DatePickerDialog(this,this,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
            dialog.show();
        }else  if(v.getId() == R.id.btn_tp_time) {
            Calendar calendar = Calendar.getInstance();
            TimePickerDialog dialog = new TimePickerDialog(this,this,calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true);
            dialog.show();
        } else if(v.getId() == R.id.btn_pg1) {
            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("我是按钮拉起的");
            progressDialog.setMessage("正在加载");
            progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    Toast.makeText(DialogActivity.this,"取消了",Toast.LENGTH_LONG).show();
                }
            });
            progressDialog.show();
        } else if(v.getId() == R.id.btn_pg2) {
            ProgressDialog progressDialog = ProgressDialog.show(this,"标题","我是内容");
            progressDialog.setTitle("我是按钮拉起的");
            progressDialog.setMessage("正在加载，2秒后自动取消");
            progressDialog.setCancelable(false);
            progressDialog.show();
            new Thread(()->{
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    Log.e("异常","异常",e);
                }
                progressDialog.dismiss();
            }).start();
        }else if(v.getId() == R.id.btn_pg3) {
            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("模拟进度");
            progressDialog.setMessage("正在加载");
             progressDialog.setCancelable(false);
             progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setMax(100);
            progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", (dialog, which) -> {
                Toast.makeText(DialogActivity.this,"按钮：OK",Toast.LENGTH_LONG).show();
            });
            progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", (dialog, which) -> {
                Toast.makeText(DialogActivity.this,"按钮：Cancel",Toast.LENGTH_LONG).show();
            });
            progressDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中性", (dialog, which) -> {
                Toast.makeText(DialogActivity.this,"按钮：中性",Toast.LENGTH_LONG).show();
            });
            progressDialog.show();
            final int[] a = {0};
            new Thread(()->{
                try {
                    while (a[0] <=100) {
                        progressDialog.setProgress(a[0]);
                       a[0] +=10;
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    Log.e("异常","异常",e);
                }
                progressDialog.dismiss();
            }).start();
        }  else if(v.getId() == R.id.btn_pg4) {
            MyDialog myDialog = new MyDialog(this,R.style.MyProgressBar1);
            myDialog.setTitle("自定义");
            myDialog.setMsg("我是提示信息");
            myDialog.setOk("OK",myDialog1 -> {
                Toast.makeText(this,"自定义提示框OK",Toast.LENGTH_SHORT).show();
            });
            myDialog.setCancel("Cancel",myDialog1 -> {
                Toast.makeText(this,"自定义提示框Cancel",Toast.LENGTH_SHORT).show();
            });
            myDialog.show();
        }
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        tv_dp.setText(String.format("DatePickerDialog选择的日期是：%s年%s月%s日",year,month,dayOfMonth));
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        tv_tp.setText(String.format("TimePickerDialog选择的日期是：%s时%s分",hourOfDay,minute));
    }
}